{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use a class when you need to encapsulate behavior and data together. Classes are ideal for creating objects that have both state (attributes) and behavior (methods)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$3.50\n"
     ]
    }
   ],
   "source": [
    "from decimal import Decimal\n",
    "\n",
    "\n",
    "class ShoppingCart:\n",
    "    def __init__(self) -> None:\n",
    "        self.items = []\n",
    "\n",
    "    def add_item(self, item: str, price: Decimal) -> None:\n",
    "        self.items.append({\"item\": item, \"price\": price})\n",
    "\n",
    "    def total(self) -> Decimal:\n",
    "        return sum(item[\"price\"] for item in self.items)\n",
    "\n",
    "\n",
    "cart = ShoppingCart()\n",
    "cart.add_item(\"apple\", Decimal(\"1.5\"))\n",
    "cart.add_item(\"banana\", Decimal(\"2.0\"))\n",
    "print(f\"${cart.total():.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A dataclass is a lightweight alternative to a class. It’s perfect for scenarios where you need to group related data but don’t need much behavior."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Item(name='apple', price=Decimal('1.5'))\n",
      "$1.50\n"
     ]
    }
   ],
   "source": [
    "from dataclasses import dataclass\n",
    "from decimal import Decimal\n",
    "\n",
    "\n",
    "@dataclass\n",
    "class Item:\n",
    "    name: str\n",
    "    price: Decimal\n",
    "\n",
    "\n",
    "item = Item(name=\"apple\", price=Decimal(\"1.5\"))\n",
    "print(item)\n",
    "print(f\"${item.price:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use a function for stateless operations: tasks that don’t require maintaining state over time and can be performed independently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$90.00\n"
     ]
    }
   ],
   "source": [
    "def calculate_discounted_price(price: Decimal, discount: Decimal) -> Decimal:\n",
    "    return price - (price * discount / 100)\n",
    "\n",
    "\n",
    "discounted_price = calculate_discounted_price(Decimal(\"100\"), Decimal(\"10\"))\n",
    "print(f\"${discounted_price:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Often, you’ll use these together for a complete solution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$3.15\n"
     ]
    }
   ],
   "source": [
    "from dataclasses import dataclass\n",
    "from decimal import Decimal\n",
    "\n",
    "\n",
    "@dataclass\n",
    "class Item:\n",
    "    name: str\n",
    "    price: Decimal\n",
    "\n",
    "\n",
    "class ShoppingCart:\n",
    "    def __init__(self) -> None:\n",
    "        self.items: list[Item] = []\n",
    "\n",
    "    def add_item(self, item: Item) -> None:\n",
    "        self.items.append(item)\n",
    "\n",
    "    def total(self) -> Decimal:\n",
    "        return sum(item.price for item in self.items)\n",
    "\n",
    "\n",
    "def calculate_discount(cart: ShoppingCart, discount: Decimal) -> Decimal:\n",
    "    return cart.total() * (1 - discount / 100)\n",
    "\n",
    "\n",
    "cart = ShoppingCart()\n",
    "cart.add_item(Item(\"apple\", Decimal(\"1.5\")))\n",
    "cart.add_item(Item(\"banana\", Decimal(\"2.0\")))\n",
    "\n",
    "discounted_price = calculate_discount(cart, Decimal(\"10\"))\n",
    "print(f\"${discounted_price:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
